class ComicMovie extends egret.DisplayObjectContainer
{
    private _sceneCache:any = {};

    private _dia:Dialog;
    private _selectDia:SelectDialog;
    private _movieConfig:ComicConfig;
    private _movieVO:ComicMovieVO;

    private _currentScene:ComicScene;
    private _currentSceneVO:ComicSceneVO;
    private _currentShotVO:ComicShotVO;

    constructor()
    {
        super();
        this.init();
    }

    private async init():Promise<void>
    {
        await this.initConfig();
        this.initUI();
        this.play();
    }
    private initUI():void
    {
        this._dia = new Dialog();
        this._selectDia = new SelectDialog();

        fairygui.GRoot.inst.addChild(this._dia);
        fairygui.GRoot.inst.addChild(this._selectDia);
        this._dia.hide();
        this._selectDia.hide();
        this._selectDia.scaleX = this._selectDia.scaleY = 1.8;
        
        if(this.stage)
        {
            this._dia.view.width = this.stage.stageWidth-5;
            this._dia.view.height = 200;
            this._dia.y = this.stage.stageHeight - 200;
            this._selectDia.stageWidth = this.stage.stageWidth;
            this._selectDia.stageHeight = this.stage.stageHeight;
        }
        this.addEventListener(egret.Event.ADDED_TO_STAGE,()=>{
            this._dia.view.width = this.stage.stageWidth-5;
            this._dia.view.height = 200;
            this._dia.y = this.stage.stageHeight - 200;
        },this)
    }
    private async initConfig():Promise<void>
    {
        await ComicResourceManager.instance.initConfig("config","resource_config_json");
        this._movieVO = ComicResourceManager.instance.movieVO;
    }

    private play():void
    {
        let firstShot:ComicShotVO = this._movieVO.startShot;
        this.playShot(firstShot);
    }

    private playNextShot(e:egret.Event):void
    {
        if(this._currentShotVO && this._currentShotVO.nextShot)
        {
            this.playShot(this._currentShotVO.nextShot);
        }
    }
    private playShot(shotVO:ComicShotVO):void
    {
        let sceneVO:ComicSceneVO = shotVO.scene;
        console.log("play,",shotVO.name)
        if(this._currentSceneVO == sceneVO)
        {
            this._currentScene.delayPlayShot(shotVO);
        }
        else
        {
            let scene:ComicScene = this.getScene(sceneVO);
            if(this._currentScene)
            {
                this._currentScene.stop();
                this.removeChild(this._currentScene);
            }
            this._currentScene = scene;
            this.addChild(this._currentScene);
            this._currentSceneVO = sceneVO;
            this._currentScene.delayPlayShot(shotVO);

        }
        this._currentShotVO = shotVO;
    }

    private getScene(sceneVO:ComicSceneVO):ComicScene
    {
        let scene:ComicScene;
        scene = this._sceneCache[sceneVO.name];
        if(scene == null)
        {
            scene = new ComicScene(sceneVO, this._dia, this._selectDia);
            scene.addEventListener(EVENT.PLAY_NEXT_SHOT, this.playNextShot,this);
            this._sceneCache[sceneVO.name] = scene;
        }
        return scene;
    }
}